package com.atguigu.mycloud.service.impl;

import com.atguigu.mycloud.Enum.ReturnMessage;
import com.atguigu.mycloud.entities.Order;
import com.atguigu.mycloud.mapper.OrderMapper;
import com.atguigu.mycloud.service.AccountFeignApi;
import com.atguigu.mycloud.service.OrderService;
import com.atguigu.mycloud.service.StorageFeignApi;
import io.seata.core.context.RootContext;
import io.seata.spring.annotation.GlobalTransactional;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

/**
 * @author 任浩
 * @version 1.0
 */
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderMapper orderMapper;
    @Resource//订单微服务通过OpenFeign去调用库存微服务
    private StorageFeignApi storageFeignApi;
    @Resource//订单微服务通过OpenFeign去调用账户微服务
    private AccountFeignApi accountFeignApi;

    //创建订单
    @Override
    @GlobalTransactional(name = "ren-create-order",rollbackFor = Exception.class) //AT
    public ReturnMessage create(Order order) {

        //首先得到全局的事务的xid，这是非常重要的
        String xid = RootContext.getXID();
        log.info("全局事务的id为：" + xid);
        System.out.println();
        log.info("开始生成新的订单");
        order.setStatus(0);  //新的订单设置状态为0，表示还没有支付完成
        int result = orderMapper.insertSelective(order);

        Order orderFromDB = null;

        if(result > 0){  //新的订单生成成功。进行库存，账户的修改
            //首先查询出当前的订单
            orderFromDB = orderMapper.selectOne(order);
            //orderFromDB = orderMapper.selectByPrimaryKey(order.getId());
            log.info("-------> 新建订单成功，orderFromDB info: "+orderFromDB);
            System.out.println();
            //2. 扣减库存
            log.info("-------> 订单微服务开始调用Storage库存，做扣减count");
            storageFeignApi.decrease(orderFromDB.getProductId(), orderFromDB.getCount());
            log.info("-------> 订单微服务结束调用Storage库存，做扣减完成");
            System.out.println();
            //3. 扣减账号余额
            log.info("-------> 订单微服务开始调用Account账号，做扣减money");
            accountFeignApi.decrease(orderFromDB.getUserId(), orderFromDB.getMoney());
            log.info("-------> 订单微服务结束调用Account账号，做扣减完成");
            System.out.println();
            //4. 修改订单状态
            //订单状态status：0：创建中；1：已完结
            log.info("-------> 修改订单状态");
            orderFromDB.setStatus(1);

            //这是修改当前用户下的订单的未支付的状态。
            Example whereCondition=new Example(Order.class);
            Example.Criteria criteria=whereCondition.createCriteria();
            criteria.andEqualTo("userId",orderFromDB.getUserId());
            criteria.andEqualTo("status",0);

            int updateResult = orderMapper.updateByExampleSelective(orderFromDB, whereCondition);

            log.info("-------> 修改订单状态完成"+"\t"+updateResult);
            log.info("-------> orderFromDB info: "+orderFromDB);
        }
        System.out.println();
        log.info("==================>结束新建订单"+"\t"+"xid_order:" +xid);
        return ReturnMessage.success("订单支付成功");
    }
}
